package tech.wetech.admin.modules.system.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tech.wetech.admin.core.annotation.SystemLog;
import tech.wetech.admin.core.utils.Result;
import tech.wetech.admin.modules.base.query.PageQuery;
import tech.wetech.admin.modules.base.web.BaseCrudController;
import tech.wetech.admin.modules.system.enums.SqlType;

import tech.wetech.admin.modules.system.po.Company;
import tech.wetech.admin.modules.system.po.UnifiedDisplay;
import tech.wetech.admin.modules.system.service.UnifiedDisplayService;
import tech.wetech.admin.modules.system.vo.DatabaseVo;
import tech.wetech.admin.modules.system.vo.KancloudVo;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @author lu
 * @description
 * @date 2020年03月19日 9:02
 */
@Controller
@RequestMapping("/display")
public class UnifiedDisplayController extends BaseCrudController<UnifiedDisplay> {

    @Autowired
    private UnifiedDisplayService unifiedDisplayService;

    @ModelAttribute("sqltype")//
    public SqlType[] resourceTypes() {
        return SqlType.values();
    }

    @GetMapping
    public String display(Model model) {
        return "system/display";
    }

    @GetMapping("/selectlist")
    @ResponseBody
    @Override
    public Result queryList(UnifiedDisplay unifiedDisplay, PageQuery pageQuery) {
        pageQuery.setOrderBy("id desc");
        List<UnifiedDisplay> list = unifiedDisplayService.queryList(unifiedDisplay, pageQuery);
        for (UnifiedDisplay display : list) {
            JSONArray parseArray = JSONArray.parseArray(display.getUrlJson());
            List<DatabaseVo> databaseVos=new ArrayList<>();
            for (Object o : parseArray) {
                databaseVos.add(JSONObject.parseObject(String.valueOf(o),DatabaseVo.class));
            }
            display.setDatabaseVos(databaseVos);
        }
        return Result.success(list)
                .addExtraIfTrue(pageQuery.isCountSql(), "total", ((Page) list).getTotal());
    }

    @ResponseBody
    @RequiresPermissions("display:create")
    @SystemLog("统一大屏新增")
    @PostMapping("/create")
    @Override
    public Result create(@Validated UnifiedDisplay unifiedDisplay) {
        Date date=new Date();
        unifiedDisplay.setCreateTime(date);
        String json=JSON.toJSONString(unifiedDisplay.getDatabaseVos());
        unifiedDisplay.setUrlJson(json);
        unifiedDisplayService.create(unifiedDisplay);
        return Result.success();
    }

    @ResponseBody
    @PostMapping("/update")
    @RequiresPermissions("display:update")
    @SystemLog("统一大屏修改")
    @Override
    public Result update(@Validated UnifiedDisplay unifiedDisplay) {
        String json=JSON.toJSONString(unifiedDisplay.getDatabaseVos());
        unifiedDisplay.setUrlJson(json);
        unifiedDisplayService.updateNotNull(unifiedDisplay);
        return Result.success();
    }

    @ResponseBody
    @SystemLog("统一大屏删除")
    @RequiresPermissions("display:delete")
    @PostMapping("/delete-batch")
    @Override
    public Result deleteBatchByIds(@RequestParam("id") Object[] ids) {
        super.deleteBatchByIds(ids);
        return Result.success();
    }
}
